<template>
    <div class="app-container">
        <div class="filter-container">
            <el-form ref="listQuery" :model="listQuery" inline>
                <el-form-item label="所属医院" prop="hid">
                    <el-select clearable class="filter-item" placeholder="请选择所属医院" v-model="listQuery.hid">
                        <el-option v-for="(item, index) in hospitalOptions" :key="item.name" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所属医生" prop="doctorId">
                    <el-select clearable class="filter-item" placeholder="请选择所属医生" v-model="listQuery.doctorId">
                        <el-option v-for="(item, index) in doctorOptions" :key="item.name" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所属分类" prop="categoryId">
                    <el-select clearable class="filter-item" placeholder="请选择所属分类" v-model="listQuery.categoryId">
                        <el-option v-for="(item, index) in categoryOptions" :key="item.name" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="洗涤码" prop="barCode">
                    <el-input size="small" clearable style="width: 150px;" class="filter-item" placeholder="请输入洗涤码" v-model="listQuery.barCode">
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="small" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
                    <el-button size="small" class="filter-item" type="danger" icon="el-icon" @click="resetFilter">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div style="height: 42px;line-height: 32px;">
            <el-button type="success" @click="handleAdd" style="float:right" size="small" icon="el-icon-plus">新增工作服</el-button>
            <el-button type="text" @click="dialogVisible=true" class="btn_tip">如何打印洗涤码？</el-button>
        </div>
        <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%" stripe>
            <el-table-column align="center" width="60px" type="index" label="序号"></el-table-column>
            <el-table-column align="center" label="ID" prop="id"></el-table-column>
            <el-table-column align="center" label="所属医院" prop="hospitalName"></el-table-column>
            <el-table-column align="center" label="所属医生" prop="doctorName"></el-table-column>
            <el-table-column align="center" label="所属分类" prop="categoryName"></el-table-column>
            <el-table-column align="center" label="洗涤码" prop="barCode"></el-table-column>
            <el-table-column align="center" label="操作" class-name="small-padding fixed-width" width="310px;">
                <template slot-scope="scope">
                    <el-button type="primary" size="small" @click="handleEdit(scope.row)" icon="el-icon-edit">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDelete(scope.row)" icon="el-icon-delete">删除</el-button>
                    <el-button type="primary" size="small" @click="print(scope.row)" icon="el-icon">打印洗涤码</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[20,50,100]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next" :total="total">
            </el-pagination>
        </div>

        <manage :clothCode="clothCode" :editOrAdd="editOrAdd" :dialogEdit.sync="dialogEdit" @refreshList="getList"></manage>

        <el-dialog title="打印提示" :visible.sync="dialogVisible" width="50%">
            <p>1.请使用windows系统的电脑</p>
            <p>2.点击<a style="color:#66b1ff;" target="_blank" href="https://pan.baidu.com/s/1dDjDSml">这里</a>下载并安装打印机驱动</p>
            <p>3.点击<a style="color:#66b1ff;" href="http://www.c-lodop.com/demolist/CLodop_Setup_for_Win32NT.zip">这里</a>下载并安装打印插件</p>
            <p>4.注意：每次打印前请确保Lodop打印插件已正确安装并启动</p>
            <p>5.注意：需要设置此打印机为默认打印机，否则打印的时候windows系统会使用Microsoft XPS Document Writer作为默认打印机，在每次打印的时候会弹出另存为xps的对话框，导致打印不成功。</p>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="dialogVisible = false">知道了</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import manage from '@/views/hospital/cloth-manage'
  import { fetchHospitalList } from '@/api/hospital'
  import { categoryGetList } from '@/api/category'
  import { doctorGetList } from '@/api/doctor'
  import { doctorClothesList, doctorClothesDelete } from '@/api/cloth'

  export default {
    name: 'HospitalCloth',
    data() {
      return {
        tableKey: 0,
        list: [],
        total: null,
        listLoading: false,
        hospitalOptions: [],
        categoryOptions: [],
        doctorOptions: [],
        listQuery: {
          page: 1,
          size: 20,
          doctorId: '',
          categoryId: '',
          barCode: '',
          hid: ''
        },
        editOrAdd: 'add',
        clothCode: '',
        dialogEdit: false,
        is_printer_available: true,
        dialogVisible: false
      }
    },
    watch: {
      'listQuery.hid'(val) {
        this.listQuery.categoryId = ''
        this.listQuery.doctorId = ''
        this.categoryOptions = []
        this.doctorOptions = []
        if (val) {
          this.getCategory()
          this.getDoctors()
        }
      }
    },
    components: {
      manage
    },
    computed: {
      ...mapGetters([
        'fid'
      ])
    },
    created: function() {
      this.getList()
      this.getHospitals()
      try {
        var CHECK_LODOP = window.getLodop()
        if (!CHECK_LODOP) {
          this.is_printer_available = false
        }
      } catch (e) {
        this.is_printer_available = false
      }
    },
    methods: {
      getCategory() {
        categoryGetList({ page: 1, size: 500, type: 2, hid: this.listQuery.hid }).then(response => {
          this.categoryOptions = response.data.list || []
        })
      },
      getDoctors() {
        doctorGetList({ page: 1, size: 500, hid: this.listQuery.hid }).then(response => {
          this.doctorOptions = response.data.list || []
        })
      },
      getHospitals() {
        fetchHospitalList({ page: 1, size: 500 }).then(response => {
          this.hospitalOptions = response.data.list || []
        })
      },
      getList() {
        this.listLoading = true
        doctorClothesList(this.listQuery).then(response => {
          this.list = response.data && response.data.list || []
          this.total = Number.parseInt(response.data.count)
          this.listLoading = false
        }).catch(e => {
          this.listLoading = false
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      resetFilter() {
        this.$refs['listQuery'].resetFields()
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleAdd() {
        this.clothCode = ''
        this.editOrAdd = 'add'
        this.dialogEdit = true
      },
      handleEdit(row) {
        this.clothCode = row.barCode
        this.editOrAdd = 'edit'
        this.dialogEdit = true
      },
      handleDelete(row) {
        this.$confirm('确定删除' + row.doctorName + '的' + row.categoryName + '?', '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          doctorClothesDelete({ id: row.id }).then(response => {
            if (response) {
              this.$message({ message: '删除成功', type: 'success', showClose: true })
              this.getList()
            }
          })
        })
      },
      print(row) {
        if (!this.is_printer_available) {
          this.$message({ message: '请先安装/启动C-Lodop打印机控件,否则无法打印', type: 'error', showClose: true })
          return
        }
        const WashcodeLODOP = window.getLodop(document.getElementById('LODOP1'), document.getElementById('LODOP_EM1'))
        WashcodeLODOP.SET_LICENSES('', 'D78E0BE18A0697159232AAC4D08037B4', 'C94CEE276DB2187AE6B65D56B3FC2848', '')
        WashcodeLODOP.SET_PRINTER_INDEXA('p1')

        WashcodeLODOP.PRINT_INITA('0mm', '0mm', '100mm', '21mm', '打印水洗码-' + row.id + '-' + row.barCode)
        WashcodeLODOP.SET_PRINT_PAGESIZE(0, '100mm', '21mm', 'barCode')
        WashcodeLODOP.SET_PRINT_STYLE('FontSize', 9)
        WashcodeLODOP.ADD_PRINT_TEXT('0mm', '52mm', '45mm', '5mm', row.departmentName + ' ' + row.hospitalName + ' ' + row.doctorName)
        WashcodeLODOP.ADD_PRINT_TEXT('0mm', '2mm', '45mm', '5mm', row.departmentName + ' ' + row.hospitalName + ' ' + row.doctorName)
        WashcodeLODOP.SET_PRINT_STYLE('FontSize', 9)
        WashcodeLODOP.ADD_PRINT_BARCODE('5mm', '2mm', '45mm', '10mm', '128Auto', row.barCode)
        WashcodeLODOP.ADD_PRINT_BARCODE('5mm', '52mm', '45mm', '10mm', '128Auto', row.barCode)
        // WashcodeLODOP.PRINT_INITA(0, 0, 400, 100, '打印水洗码-' + row.id + '-' + row.barCode)
        // WashcodeLODOP.SET_PRINT_PAGESIZE(0, 400, 100, 'barCode')
        // WashcodeLODOP.SET_PRINT_STYLE('FontSize', 9)
        // WashcodeLODOP.ADD_PRINT_TEXT(10, 10, 160, 30, row.departmentName + ' ' + row.hospitalName + ' ' + row.doctorName)
        // WashcodeLODOP.ADD_PRINT_TEXT(10, 260, 160, 30, row.departmentName + ' ' + row.hospitalName + ' ' + row.doctorName)
        // WashcodeLODOP.SET_PRINT_STYLE('FontSize', 9)
        // WashcodeLODOP.ADD_PRINT_BARCODE(30, 10, 160, 40, '128Auto', row.barCode)
        // WashcodeLODOP.ADD_PRINT_BARCODE(30, 260, 160, 40, '128Auto', row.barCode)

        // WashcodeLODOP.PREVIEW()
        WashcodeLODOP.PRINT()
      }
    }
  }
</script>

<style scoped>
    .btn_tip {
        float: right;
        margin-right: 20px;
    }
</style>
